package com.example.demo.controlller;

import com.aliyuncs.utils.StringUtils;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.medicaluser;
import com.example.demo.service.LoginRegisterservice;

import com.example.demo.utils.CaptureConfig;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Locale;


@RestController
public class LoginRegisterController {

    @Autowired
   LoginRegisterservice loginRegisterservice;


    @GetMapping("/login")
    @CrossOrigin(origins = {"1"}, allowCredentials = "true")
    public Result login(String username, String password, @RequestParam("verificationCode") String verificationCode,HttpSession session){
        String captcha = (String) session.getAttribute("captcha");
        System.out.println(captcha);
        if (StringUtils.isEmpty(verificationCode) || !captcha.equalsIgnoreCase(verificationCode)) {
            return Result.error("验证码错误，请重新输入！");
        }

        try {
            String jwt = loginRegisterservice.login(username, password);
            return Result.success(jwt);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @RequestMapping("/register")
    public Result register(@RequestBody medicaluser user){
        try {
            loginRegisterservice.register(user);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

}
